package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Permission;
import cn.wolfcode.wms.mapper.PermissionMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IPermissionService;
import cn.wolfcode.wms.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class PermissionService implements IPermissionService {


    //因为所有的controller都交给spring管理所以要一个spring对象里面有所有的controller
    @Autowired
    private ApplicationContext ctx;


    @Autowired
    private PermissionMapper mapper;

    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int insert(Permission record) {
        return mapper.insert(record);
    }

    public Permission selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Permission> selectAll() {
        return mapper.selectAll();
    }

    public int updateByPrimaryKey(Permission record) {
        return mapper.updateByPrimaryKey(record);
    }


    public void saveorupdate(Permission permission) {
        if (permission.getId() != null) {
            mapper.updateByPrimaryKey(permission);
        } else {
            mapper.insert(permission);
        }
    }

    public void delete(Long id) {
        if (id != null) {
            mapper.deleteByPrimaryKey(id);
        }


    }


    public PageResult query(QueryObject qo) {
        int count = mapper.queryForCount(qo);
        if (count == 0) {
            return PageResult.empty(qo.getPageSize());
        }
        List<Permission> list = mapper.queryForList(qo);

        PageResult pageResult = new PageResult(qo.getPageSize(), count, qo.getCurrentPage(), list);


        return pageResult;
    }

    public void load(Long id) {
        //先获取到系统已经有的所有权限
        List<Permission> permissions = mapper.selectAll();
        Set<String> expreeions = new HashSet<>();
        //将这些权限放在set集合中去重

        for (Permission permission : permissions) {
            expreeions.add(permission.getExpression());
        }



        //先获取所有 贴有 controller 注解 所有的bean
        Map<String, Object> objectMap = ctx.getBeansWithAnnotation(Controller.class);
        //再获取controller中所有的的方法
        for (Object bean : objectMap.values()) {
            //遍历所有的并且是获取所有的公共的方法(Declared 公共的,公然的)
            Method[] methods = bean.getClass().getDeclaredMethods();
            for (Method method : methods) {
                //判断方法中所有看是否贴有权限的注解
                if (method.isAnnotationPresent(RequiredPermission.class)) {

                    //获取权限注解
                    RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                    System.out.println(annotation+"111111111111111111111111111111111111111111111111111111111");
                    //如果有注解就拼接权限表达式,
                    String expresion = bean.getClass().getName() + ":" + method.getName();
                    System.out.println(expresion+"22222222222222222222222222222222222222222222222222222222222");

                    //判断当前系统中是否已经纯在表达式
                    if(!expreeions.contains((expresion))){
                        String name = annotation.value();
                        //封装权限对象
                        Permission permission = new Permission();
                        permission.setExpression(expresion);
                        permission.setName(name);
                        //保存权限
                        mapper.insert(permission);
                    }
                }

            }

        }

    }
}

